import VueCookies from "vue-cookies";
import queryString from "query-string";
import store from "store";
import { getRiseColor } from "utils/native";
// import { initThemes } from "src/theme";

import cache from "utils/cache";
// import themePrimaryList from "src/theme/data/theme-data";
const version = require("../../version.json").version;
// 引入jyChangeTheme方法
import { jyChangeTheme } from "GildataDesign2/lib/utils/change-theme.js";
/*新增色号兼容*/
const color = {
  White: ["White", "white", "light"],
  //炫金主题
  Blackv1: ["Black", "dark", "Dark", "black", "Blackv1", "darkv1", "blackv1"],
  // Golden: ["Golden", "golden", "gold"],
  // Blackv1: ["Blackv1", "darkv1", "blackv1"],
  Blue: ["Blue"],
  OldBlack: ["night"],
  OldWhite: ["plain"],
  // JCDYBlack:['JCDYBlack','jcdyblack'],
  // JCDYWhite:['JCDYWhite','jcdywhite']
  lightBlue: ["lightBlue"],
  darkBlue: ["darkBlue"],
};

export const getTheme = (type = "White") => {
  let result = "White";
  Object.keys(color).map((item) => {
    if (color[item].indexOf(type) > -1) {
      result = item;
    }
    return item;
  });
  return result;
};
/**
 * 修改主题
 */
const updateTheme = async (type = "White") => {
  console.log("updateTheme", type);
  type = getTheme(type);
  document.getElementById(
    "themeStyle"
  ).href = `./style/${type}.base.css?date=${version}`;
  VueCookies.set("themeType", type, -1);
  store.dispatch("changeThemeType", type);
  const riseColor = await getRiseColor();
  VueCookies.set("riseColor", riseColor, -1);
  // 切换黑色主题
  jyChangeTheme(type);
  updateRiseColor();
  console.log("type", type);

  // if (process.env.VUE_APP_BD !== "excel") {
  //   initThemes(type === "White" ? "lightBlue" : "darkYellow", themePrimaryList);
  // }
};
/**
 * 修改字体
 */
const updateFontSize = (data) => {
  const fontArray = [12, 13, 14, 15, 16, 18, 20, 24];
  fontArray.map((item) => {
    document.body.style.setProperty(`--size_${item}`, `${item + data - 12}px`);

    return item;
  });
  console.log("data", data);
  document.documentElement.style.fontSize = `${data}px`;
  VueCookies.set("zjs_fontSize", data, -1);
  store.dispatch("updateFontSize", data);
};

/**
 * 初始化主题
 */
export const initTheme = (type) => {
  let themeType = "";
  let defaultTheme = "White";
  if (!type) {
    const hrefArray = window.location.href.split("?");

    if (hrefArray[1]) {
      themeType = queryString.parse(hrefArray[1]).themeType;
    }
    // let defaultTheme = "Black";
    if (
      process.env.VUE_APP_MODULE == "report_pro" ||
      process.env.VUE_APP_MODULE == "report_test"
    ) {
      defaultTheme = "White";
    }
  } else {
    themeType = type;
  }
  console.log("11111");
  themeType =
    themeType ||
    (VueCookies.isKey("themeType")
      ? VueCookies.get("themeType")
      : defaultTheme);
  updateTheme(themeType);
  // 切换黑色主题
  jyChangeTheme(themeType);
};
/**
 * 初始化字体
 */
export const initSize = (size = 12) => {
  const fontSize =
    size ||
    (VueCookies.isKey("zjs_fontSize") ? +VueCookies.get("zjs_fontSize") : 12);
  updateFontSize(fontSize);
};

/**
 * 红涨绿跌切换
 */
export const updateRiseColor = (color) => {
  if (!color) color = VueCookies.get("riseColor");
  const themeType = VueCookies.get("themeType");
  const riseObj = {
    Blackv1: ["#ff4650", "#80dc72"],
    White: ["#e13e36", "#219421"],
  };
  const colorList = riseObj[themeType];
  const result = color === "red" ? colorList : colorList.reverse();
  document.documentElement.style.setProperty("--function_01", result[0]);
  document.documentElement.style.setProperty("--function_02", result[1]);
  VueCookies.set("riseColor", color, -1);
  store.dispatch("updateRiseColor", color);
};

/**
 * 初始化保存用户id
 */
export const initUserId = () => {
  const hrefArray = window.location.href.split("?");
  if (hrefArray[1]) {
    const userId = queryString.parse(hrefArray[1]).userId;
    if (userId) VueCookies.set("userId", userId, -1);
  }
};

/**
 * 界面初始化
 */
export const initPage = (type) => {
  console.log("0000", type);
  // 初始化主题
  initTheme(type);
  // 保存用户id
  initUserId();
  //初始化 添加请求前缀
  initRequestPrefix();
  // 初始化字体
  // alert(process.env.VUE_APP_ENV);
  if (
    process.env.VUE_APP_MODULE == "report_pro" ||
    process.env.VUE_APP_MODULE == "report_test"
  ) {
    initSize(14);
  } else {
    initSize();
  }
};

/**
 * 初始化自定义请求前缀
 */
export const initRequestPrefix = () => {
  const hrefArray = window.location.href.split("?");
  let Hostkey = "",
    params = {};
  if (hrefArray[1]) {
    Hostkey = queryString.parse(hrefArray[1]).Host;
    params = queryString.parse(hrefArray[1]);
    if (
      !Hostkey &&
      Object.keys(params).includes("Host") &&
      cache.getLocal("Host")
    ) {
      cache.removeLocal("Host");
    } else if (Hostkey) {
      cache.setLocal("Host", Hostkey);
    }
  }
};

export const getHost = (host) => {
  return cache.getLocal("Host") ? cache.getLocal("Host") : null;
};

/**
 * 接收native端事件调用
 */
export const nativeActions = (type, data) => {
  console.log(type, data);
  switch (type) {
    case "themeType":
      return updateTheme(data);
    case "fontSize":
      return updateFontSize(data);
    case "riseColor":
      return updateRiseColor(data);
  }
};
